<?php
// logger tools

require_once dirname(__FILE__).'/Logger.php';
require_once dirname(__FILE__).'/NewsReply.php';
require_once dirname(__FILE__).'/Item.php';

$logger = Logger::getInstance();

header('Content-Type: text/xml; charset=UTF-8');
$settings = parse_ini_file ( "config.ini" );
//$logger->debug ( $settings ['db.host']);
//echo $settings ['db.host']."<br/>";
$locationIdentify = $settings['location.identify'];

$postOrGet = $_SERVER ["REQUEST_METHOD"];

if (strcasecmp ( $postOrGet, "POST" ) == 0) { // post method
	$xmlStr = file_get_contents('php://input');
	if(strpos($xmlStr, $locationIdentify) == false){//text message
		$logger->debug($xmlStr);
		$xml = simplexml_load_string($xmlStr);
		$wechatUser = $xml->FromUserName;
		$messageContent = $xml->Content;
		$logger->debug($wechatUser.' send message '.$messageContent);
		//record
		
		$newsReply = new NewsReply();
		$newsReply->_fromUserName = 'shanghaiweather';
		$newsReply->_toUserName = $wechatUser;
		
		$newsReply->_content = "shanghai weather";
		$item = new Item();
		
 		$newsReply->_article = $item->getShanghaiWeatherItems(1);
		
 		$newsReply->_articleCount = count($newsReply->_article);
 		
 		$replyMsg = $newsReply->toXmlString();
 		$logger->debug(' reply message '.$replyMsg);
		echo $replyMsg;
	}
	else if (strpos($xmlStr, $locationIdentify) != false){//location message
		
	}
	
} else if (strcasecmp ( $postOrGet, "GET" ) == 0) {
	if (! isset ( $_GET ["echostr"] )) {
		$logger->debug ( "No echoStr get method" );
		echo "No echoStr get method";
	} else {
		$echostr = $_GET ["echostr"];
		$logger->debug ( "Get Method echostr" . $echostr );
		echo $echostr;
	}
} else {
	$logger->error ( "Not get method or post method." );
}

?>